package org.jeecg.client;

import org.jeecg.client.tcp.TCPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 请描述类的业务用途
 * @Author wangyi
 * @Date 2022/9/7 15:58
 * @Version 1.0
 **/
public class SendUtils {

    private static final Logger Log = LoggerFactory.getLogger("U_Log");

    public static DataSet sendRunDbCmd(String sql) throws Exception {
        ClientMng instance = ClientMng.getInstance();
        DataSet response = null;
        // 执行数据集命令
        Map<String, String> parameters = new HashMap<>();
        parameters.put("sql", sql);
        BizRequest request = instance.createRequest("service", "", "", "querydb", parameters);
        Log.debug("--------Start------------");
        Log.debug(request.toString());
        try{
            response = instance.RunDbCmd(request);
            Log.debug(response.toString());
            Log.debug("--------End------------");
            Log.debug("");
//            List<DataRow> rows = response.getTable(0).getRows();
//            rows.forEach( o -> {
//                Map<String, String> fields = o.getFields();
//                for(Map.Entry<String,String> m :fields.entrySet()){
//                    System.out.println(m.getKey()+" -------- "+m.getValue());
//                }});
        }catch(Exception ex){
            throw new Exception(String.format("Command [%s] failed: %s", request.getCmdName(), ex.getMessage()));
        }
        try {
            //ClientMng.Instance.close();
        }catch (Exception e){

        }
        return response;
    }

    public static BizResponse sendRunCmd(Map<String,String> map) throws Exception {

        ClientMng instance = ClientMng.getInstance();
        BizRequest request = instance.createRequest(map.remove("serviceid"), map.remove("clientid"), map.remove("entityid"), map.remove("cmd"), map);
        Log.debug("--------Start------------");
        Log.debug(request.toString());
        BizResponse response = instance.RunCmd(request);
        Log.debug(response.toString());
        Log.debug("--------End------------");
        Log.debug("");

        if (!BizResponse.OK.equals(response.getErrorCode()))
            throw new Exception(String.format("Command [%s] failed: %s  %s", request.getCmdName(), response.getErrorCode(), response.getErrorMessage()));
        return response;
    }

//    public static void main(String[] args) throws Exception {
//        Map map = new HashMap<>();
//        map.put("userid","Admin");
//        map.put("password","1");
//
//        BizResponse bizResponse = sendRunCmd(map);
//        System.out.println(bizResponse);
//    }

    public static BizResponse runCmdDj(Map<String, String> map)  throws Exception {
        ClientMng instance = ClientMng.getInstance();
        BizRequest request = instance.createRequest(map.remove("serviceid"), map.remove("clientid"), map.remove("entityid"), map.remove("cmd"),map);
        Log.debug("--------Start------------");
        Log.debug(request.toString());
        BizResponse response = instance.RunCmd(request);
        Log.debug(response.toString());
        Log.debug("--------End------------");
        Log.debug("");
        if (!BizResponse.OK.equals(response.getErrorCode()))
            throw new Exception(String.format("Command [%s] failed: %s  %s", request.getCmdName(), response.getErrorCode(), response.getErrorMessage()));
        return response;
    }

    public static BizResponse runOpc(Map<String, String> map)  throws Exception {
        return null;
    }

    public static BizResponse runOpcWrite(Map<String, String> map)  throws Exception {
        return null;
    }

    public static void main(String[] args) throws Exception {
        runOpc(new HashMap<>());
        runOpcWrite(new HashMap<>());
    }
}
